package com.happok.onlineclass.usermanagement.setting.service.impl;

import com.happok.onlineclass.usermanagement.common.exception.GoneException;
import com.happok.onlineclass.usermanagement.common.utils.Sm4Util;
import com.happok.onlineclass.usermanagement.setting.dto.*;
import com.happok.onlineclass.usermanagement.setting.po.SmsPO;
import com.happok.onlineclass.usermanagement.setting.service.EmailService;
import com.happok.onlineclass.usermanagement.setting.service.FunctionSettingService;
import com.happok.onlineclass.usermanagement.setting.service.SettingService;
import com.happok.onlineclass.usermanagement.setting.service.SmsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 接口分类                        yapi 分类文件夹
 * 功能说明
 *
 * @author : chenac
 * @module 项目名称/模块名          yapi 项目组/子项目
 * @date : 2021/10/9 13:52
 */
@Service
public class SettingServiceImpl implements SettingService {

    @Autowired
    private EmailService emailService;

    @Autowired
    private SmsService smsService;

    @Autowired
    private FunctionSettingService functionSettingService;

    @Override
    public List<FunctionStatusDTO> getFunctionStatus() {
        boolean smsEnabled = smsSettingEnable(getSmsSettingEnable());
        boolean emailEnabled = emailSettingEnable(getEmailSetting(false));
        if (!smsEnabled && !emailEnabled) {
            return new ArrayList<>();
        }
        List<FunctionVerifyDTO> functionVerifyDTOS = functionSettingService.listFunctionSetting();
        if (ObjectUtils.isEmpty(functionVerifyDTOS)) {
            return new ArrayList<>();
        }
        List<FunctionStatusDTO> functionStatusDTOS = new ArrayList<>();
        for (FunctionVerifyDTO functionVerify : functionVerifyDTOS) {
            FunctionStatusDTO functionStatusDTO = new FunctionStatusDTO();
            BeanUtils.copyProperties(functionVerify, functionStatusDTO);
            functionStatusDTO.setFunctionUsable(functionVerify.getOpen());
            if (emailEnabled &&
                    !ObjectUtils.isEmpty(functionVerify.getEmailVerify().getTopic()) &&
                    !ObjectUtils.isEmpty(functionVerify.getEmailVerify().getContent())) {
                functionStatusDTO.setEmailUsable(true);
            } else {
                functionStatusDTO.setEmailUsable(false);
            }
            if (smsEnabled &&
                    !ObjectUtils.isEmpty(functionVerify.getSmsVerify().getTemplateCode())) {
                functionStatusDTO.setSmsUsable(true);
            } else {
                functionStatusDTO.setSmsUsable(false);
            }
            functionStatusDTOS.add(functionStatusDTO);
        }
        return functionStatusDTOS;
    }

    @Override
    public void updateEmailSetting(EmailSettingDTO emailSetting) {
        emailService.updateEmailSetting(emailSetting);
    }

    @Override
    public EmailSettingInfoDTO getEmailSetting(boolean encrypt) {
        return emailService.getEmailSetting(encrypt);
    }

    @Override
    public void updateSmsSetting(SmsSettingDTO smsSetting) {
        smsService.updateSmsSetting(smsSetting);
    }

    @Override
    public List<SmsSettingInfoDTO> listSmsSetting() {
        return smsService.listSmsSetting();
    }

    @Override
    public void smsEnable(SmsEnableDTO smsEnable) {
        smsService.smsEnable(smsEnable);
    }

    @Override
    public void openOrCloseUserRegister(UserRegisterStatusDTO userRegisterStatusDTO) {
        functionSettingService.openOrCloseUserRegister(userRegisterStatusDTO);
    }

    @Override
    public void updateUserRegister(UpdateVerifyDTO updateVerify) {
        functionSettingService.updateFunctionSetting(FunctionClassifyEnum.USER_REGIST, updateVerify);
    }

    @Override
    public FunctionVerifyDTO getUserRegister() {
        return functionSettingService.getFunctionSetting(FunctionClassifyEnum.USER_REGIST);
    }

    @Override
    public void updateUserLogin(UpdateVerifyDTO updateVerify) {
        functionSettingService.updateFunctionSetting(FunctionClassifyEnum.USER_LOGIN, updateVerify);
    }

    @Override
    public FunctionVerifyDTO getUserLogin() {
        return functionSettingService.getFunctionSetting(FunctionClassifyEnum.USER_LOGIN);
    }

    @Override
    public void updateForgetPwd(UpdateVerifyDTO updateVerify) {
        functionSettingService.updateFunctionSetting(FunctionClassifyEnum.FORGET_PASSWORD, updateVerify);
    }

    @Override
    public FunctionVerifyDTO getForgetPwd() {
        return functionSettingService.getFunctionSetting(FunctionClassifyEnum.FORGET_PASSWORD);
    }

    @Override
    public void updateResetPwd(UpdateVerifyDTO updateVerify) {
        functionSettingService.updateFunctionSetting(FunctionClassifyEnum.RESET_PASSWORD, updateVerify);
    }

    @Override
    public FunctionVerifyDTO getResetPwd() {
        return functionSettingService.getFunctionSetting(FunctionClassifyEnum.RESET_PASSWORD);
    }

    @Override
    public void updateModifyAccount(UpdateVerifyDTO updateVerify) {
        functionSettingService.updateFunctionSetting(FunctionClassifyEnum.MODIFY_ACCOUNT, updateVerify);
    }

    @Override
    public FunctionVerifyDTO getModifyAccount() {
        return functionSettingService.getFunctionSetting(FunctionClassifyEnum.MODIFY_ACCOUNT);
    }

    @Override
    public void updateAddUser(UpdateVerifyDTO updateVerify) {
        functionSettingService.updateFunctionSetting(FunctionClassifyEnum.ADD_USER, updateVerify);
    }

    @Override
    public FunctionVerifyDTO getAddUser() {
        return functionSettingService.getFunctionSetting(FunctionClassifyEnum.ADD_USER);
    }

    @Override
    public SmsPO getSmsSettingEnable() {
        SmsPO smsPO = smsService.getEnableSmsSetting();
        if (ObjectUtils.isEmpty(smsPO)) {
            return null;
        }
        smsPO.setAccessKeySecret(Sm4Util.decrypt(smsPO.getAccessKeySecret()));
        return smsPO;
    }

    @Override
    public Boolean smsSettingEnable(SmsPO smsSetting) {
        if (ObjectUtils.isEmpty(smsSetting)) {
            return false;
        } else {
            if (ObjectUtils.isEmpty(smsSetting.getAccessKeyId()) ||
                    ObjectUtils.isEmpty(smsSetting.getAccessKeySecret()) ||
                    ObjectUtils.isEmpty(smsSetting.getDomainName()) ||
                    ObjectUtils.isEmpty(smsSetting.getSmsPlatform())) {
                return false;
            }
        }
        return true;
    }

    @Override
    public Boolean emailSettingEnable(EmailSettingInfoDTO emailSettingInfoDTO) {
        if (ObjectUtils.isEmpty(emailSettingInfoDTO)) {
            return false;
        } else {
            if (ObjectUtils.isEmpty(emailSettingInfoDTO.getServer()) ||
                    ObjectUtils.isEmpty(emailSettingInfoDTO.getAccount()) ||
                    (ObjectUtils.isEmpty(emailSettingInfoDTO.getPassword()) &&
                            ObjectUtils.isEmpty(emailSettingInfoDTO.getAuthorizationCode()))) {
                return false;
            }
        }
        return true;
    }

    @Override
    public FunctionVerifyDTO getFunctionSetting(FunctionClassifyEnum functionClassify) {
        return functionSettingService.getFunctionSetting(functionClassify);
    }

    @Override
    public void checkFunctionAndSetting(String phone, String email, FunctionClassifyEnum functionClassify) {
        FunctionVerifyDTO functionSetting = functionSettingService.getFunctionSetting(functionClassify);
        if (ObjectUtils.isEmpty(functionSetting) || !functionSetting.getOpen()) {
            throw new GoneException(1, "功能设置异常");
        }
        if (!ObjectUtils.isEmpty(phone)) {
            SmsPO smsSetting = getSmsSettingEnable();
            Boolean smsEnable = smsSettingEnable(smsSetting);
            if (!smsEnable) {
                throw new GoneException(1, "短信服务设置异常");
            }
            if (ObjectUtils.isEmpty(functionSetting.getSmsVerify().getTemplateCode())) {
                throw new GoneException(1, "功能设置异常");
            }
        } else {
            EmailSettingInfoDTO emailSetting = getEmailSetting(false);
            Boolean emailEnable = emailSettingEnable(emailSetting);
            if (!emailEnable) {
                throw new GoneException(1, "邮箱服务设置异常");
            }
            if (ObjectUtils.isEmpty(functionSetting.getEmailVerify().getContent()) ||
                    ObjectUtils.isEmpty(functionSetting.getEmailVerify().getTopic())) {
                throw new GoneException(1, "功能设置异常");
            }
        }
    }

    @Override
    public Boolean functionEnable(FunctionVerifyDTO functionVerify) {
        if (!functionVerify.getOpen()) {
            return false;
        }
        if ((ObjectUtils.isEmpty(functionVerify.getEmailVerify().getContent()) ||
                ObjectUtils.isEmpty(functionVerify.getEmailVerify().getTopic())) ||
                ObjectUtils.isEmpty(functionVerify.getSmsVerify().getTemplateCode())) {
            return false;
        }
        return true;
    }
}